{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 应用程序，给图识物\n",
    "#### 前向传播执行应用（三步）\n",
    "\n",
    "    predict(输入特征, batch_size=整数)\n",
    "    返回前向传播计算结果\n",
    "    \n",
    "    model = tf.keras.models.Sequential([                 #复现模型-搭建网络（前向传播）\n",
    "        tf.keras.layers.Flatten(),\n",
    "        tf.keras.layers.Dense(128, activation='relu'),\n",
    "        tf.keras.layers.Dense(10, activation='softmax’)])\n",
    "        \n",
    "    model.load_weights(model_save_path)                 #加载参数，读取已有参数\n",
    "    result = model.predict(x_predict)                  #预测结果，根据输入特征预测结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "type = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']\n",
    "\n",
    "model_save_path = './checkpoint/fashion.ckpt'\n",
    "\n",
    "model = tf.keras.models.Sequential([\n",
    "    tf.keras.layers.Flatten(),\n",
    "    tf.keras.layers.Dense(128, activation='relu'),\n",
    "    tf.keras.layers.Dense(10, activation='softmax')\n",
    "])\n",
    "                                        \n",
    "#载入已有参数\n",
    "model.load_weights(model_save_path)\n",
    "\n",
    "#输入图片\n",
    "preNum = int(input(\"input the number of test pictures:\"))\n",
    "for i in range(preNum):\n",
    "    image_path = input(\"the path of test picture:\")\n",
    "    img = Image.open(image_path)\n",
    "    \n",
    "    ##################预处理#####################\n",
    "    # 使用resize改变图片尺寸\n",
    "    img=img.resize((28,28),Image.ANTIALIAS)\n",
    "    # 转换为灰度图\n",
    "    img_arr = np.array(img.convert('L'))\n",
    "    #训练集使用灰底白字，输入图片是白底黑字，所以需要将像素值反转，满足输入要求\n",
    "    img_arr = 255 - img_arr  #每个像素点= 255 - 各自点当前灰度值\n",
    "    ###############增强图片对比度################\n",
    "#     for i in range(28):\n",
    "#         for j in range(28):\n",
    "#             if img_arr[i][j] < 200:\n",
    "#                 img_arr[i][j] = 255\n",
    "#             else:\n",
    "#                 img_arr[i][j] = 0\n",
    "    ############################################\n",
    "    #归一化\n",
    "    img_arr = img_arr/255.0\n",
    "    #img_arr添加一个维度从[28,28]变为[1,28,28]\n",
    "    x_predict = img_arr[tf.newaxis,...]\n",
    "\n",
    "    result = model.predict(x_predict)\n",
    "    #选出第一列的最大概率值并输出\n",
    "    pred=tf.argmax(result, axis=1)\n",
    "    \n",
    "    print('\\n')\n",
    "    print(type[int(pred)])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
